<?php


class MY_Json {
	
	function __construct()
	{
		
	}
	
	function to_json($array)
	{
		foreach($array as $key=>$value)
		{
			$array[$key]=urlencode($value);
		}
		return urldecode(json_encode($array));
	}
	
	function get_page($per_page, $page_number, $result, $tot_rows){
		$ret["totlePage"] = $tot_rows / $per_page;
		if($tot_rows % $per_page)
			$ret["totlePage"]++;
		$ret["nowPage"] = $page_number;
		$cnt = 0;
		foreach ($result as $row){
			$ret["title"][$cnt] = $row->title;
			$ret["date"][$cnt] = $row->time;
			$ret["ID"][$cnt] = $row->ID;
			$cnt++;
		}
		
		$temp1 = ""; $temp2 = "";
		for($i = 0; $i < $cnt-1; $i++){
			$temp1 = sprintf("{\"id\":\"%s\",\"title\":\"%s\",\"data\":\"%s\"}",$ret["ID"][$i], $ret["title"][$i], $ret["date"][$i]);
			$temp2 = $temp2 . $temp1 . ",";
		}
		$temp1 = sprintf("{\"id\":\"%s\",\"title\":\"%s\",\"data\":\"%s\"}", $ret["ID"][$cnt-1], $ret["title"][$cnt-1], $ret["date"][$cnt-1]);
		$temp2 = $temp2 . $temp1;
		
		$ret_string = sprintf("{\"totlePage\":\"%d\",\"nowPage\":\"%d\",\"content\":[%s]}", $ret["totlePage"], $ret["nowPage"], $temp2);
		return $ret_string;
	}
	
}

?>